﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserControl_ControlKhaiBaoSanPham : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag"] == null)
            {
                ViewState["flag"] = false;
            }
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            try
            {
                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                TxtMaSP.Text = grv.Cells[0].Text;
                txtMoTa.Text = grv.Cells[1].Text;
                ViewState["flag"] = false;
                GridView1.DataSource = null;
                GridView1.DataBind();
                TabContainer1.ActiveTabIndex = 0;
            }
            catch
            {
            }
        }
    }
    void dataGrid()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[SEARCH_INPRODCT]", CommandType.StoredProcedure,
            new SqlParameter("@parameter", txtFind.Text));
        if (db.Rows.Count > 0)
        {
            GridView1.DataSource = db;
            GridView1.DataBind();
        }
        else
        {
            uscMsgBox1.AddMessage("Không tìm thấy dữ liệu tương ứng", uscMsgBox.enmMessageType.Success);
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        dataGrid();
    }
    protected void ImgNew_Click(object sender, ImageClickEventArgs e)
    {
        TxtMaSP.Text = "auto";
        txtMoTa.Text = "";
        ViewState["flag"] = true;
    }
    protected void ImgSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        if ((bool)ViewState["flag"])
        {
            TxtMaSP.Text = dt.GetDataDepend(str, "[k_insertProduct]", CommandType.StoredProcedure, ref error,
                new SqlParameter("@description", txtMoTa.Text));
            ViewState["flag"] = false;
            uscMsgBox1.AddMessage("Khai báo sản phẩm thành công", uscMsgBox.enmMessageType.Success);
        }
        else
        {
            bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[updateInprodct]", ref error,
                new SqlParameter("@productkey", TxtMaSP.Text),
                new SqlParameter("@description", txtMoTa.Text));
            if (kq)
            {
                uscMsgBox1.AddMessage("Cập nhật thông tin thành công", uscMsgBox.enmMessageType.Success);
            }
            else
            {
                uscMsgBox1.AddMessage("Cập nhật thông tin thất bại", uscMsgBox.enmMessageType.Error);
            }
        }
    }
    protected void ImgFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void btFind_Click(object sender, EventArgs e)
    {
        dataGrid();
    }
}